package view;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.util.*;
import logic.Counter;
import logic.Duck;
import logic.FieldStats;
import logic.Fox;
import logic.Grass;
import logic.Hunter;
import logic.Rabbit;
import logic.Simulator;

/**
 * This View shows a table. this table has seperate lines 
 * for several kinds of actors.
 * @author Vossen en Konijnen group 2
 *
 */
public class LineView extends AbstractView {
	private ArrayList<Integer> rabbitCount;
	private ArrayList<Integer> hunterCount;
	private ArrayList<Integer> duckCount;
	private ArrayList<Integer> foxCount;
	private ArrayList<Integer> grassCount;
	private int step;
	int i;
	int x;
	private FieldStats stats;
	
	/**
	 * Makes a new LineView.
	 * Also adds this view to the list of views in AbstractModel.
	 * @param simulator
	 */
	public LineView(Simulator simulator) {
		super(simulator);
		setSize(500,500);
		rabbitCount = new ArrayList<Integer>();
		foxCount = new ArrayList<Integer>();
		hunterCount = new ArrayList<Integer>();
		duckCount = new ArrayList<Integer>();
		grassCount = new ArrayList<Integer>();
		i = 0;
		x = 0;

	}
		
	/**
	 * Paint's the LineView.
	 */
	public void paintComponent(Graphics g) {
			g.setColor(new Color(238,238,238));
			Dimension d=getSize();
			g.fillRect(0,0,d.width,d.height);
			stats = getModel().getStats();
			HashMap hashmap = stats.getHashMap();
			step = getModel().getStep();
			if(step == 0){
					rabbitCount.clear();
					foxCount.clear();
					hunterCount.clear();
					duckCount.clear();
					grassCount.clear();
			}
			try
			{
				rabbitCount.add((Integer)((Counter) hashmap.get(Rabbit.class)).getCount());
				foxCount.add((Integer) ((Counter) hashmap.get(Fox.class)).getCount());
				hunterCount.add((Integer) ((Counter) hashmap.get(Hunter.class)).getCount());
				duckCount.add((Integer) ((Counter) hashmap.get(Duck.class)).getCount());
				grassCount.add((Integer) ((Counter) hashmap.get(Grass.class)).getCount());

			}
			catch(Exception e)
			{
				
				
			}
			if(!(rabbitCount.isEmpty() && foxCount.isEmpty() && hunterCount.isEmpty() && duckCount.isEmpty() && grassCount.isEmpty()))
			{

				if(step == 1)
				{
					g.setColor(Color.ORANGE);
					g.drawLine((step-1),rabbitCount.get(0)/ 5,step,rabbitCount.get(1)/ 5);
					g.setColor(Color.BLUE);
					g.drawLine((step-1),foxCount.get(0)/ 5,step,foxCount.get(1)/ 5);
					g.setColor(Color.BLACK);
					g.drawLine((step-1),hunterCount.get(0)/ 5,step,hunterCount.get(1)/ 5);
					g.setColor(Color.RED);
					g.drawLine((step-1),duckCount.get(0)/ 5,step,duckCount.get(1)/ 5);
					g.setColor(Color.GREEN);
					g.drawLine((step-1),grassCount.get(0)/ 5,step,grassCount.get(1)/ 5);
				}
				else if(step > 1 && step < 320)				
				{
					x=1;
					while(x < rabbitCount.size())
					{
						g.setColor(Color.ORANGE);
						g.drawLine((x-1),rabbitCount.get(x-1)/ 5,x,rabbitCount.get(x)/ 5);
						g.setColor(Color.BLUE);
						g.drawLine((x-1),foxCount.get(x-1)/ 5,x,foxCount.get(x)/ 5);
						g.setColor(Color.BLACK);
						g.drawLine((x-1),hunterCount.get(x-1)/ 5,x,hunterCount.get(x)/ 5);
						g.setColor(Color.RED);
						g.drawLine((x-1),duckCount.get(x-1)/ 5,x,duckCount.get(x)/ 5);
						g.setColor(Color.GREEN);
						g.drawLine((x-1),grassCount.get(x-1) / 5,x,grassCount.get(x)/ 5);
						x++;
					}
				}
				else if(step > 320)
				{
					x=1;
					while(x < rabbitCount.size())
					{
						g.setColor(Color.ORANGE);
						g.drawLine((x-1),rabbitCount.get(x-1)/ 5,x,rabbitCount.get(x)/ 5);
						g.setColor(Color.BLUE);
						g.drawLine((x-1),foxCount.get(x-1)/ 5,x,foxCount.get(x)/ 5);
						g.setColor(Color.BLACK);
						g.drawLine((x-1),hunterCount.get(x-1)/ 5,x,hunterCount.get(x)/ 5);
						g.setColor(Color.RED);
						g.drawLine((x-1),duckCount.get(x-1)/ 5,x,duckCount.get(x)/ 5);
						g.setColor(Color.GREEN);
						g.drawLine((x-1),grassCount.get(x-1)/ 5,x,grassCount.get(x)/ 5);
						x++;
					}
					rabbitCount.remove(0);
					foxCount.remove(0);
					hunterCount.remove(0);
					duckCount.remove(0);
					grassCount.remove(0);
				}
			}
		}		
}